﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Fcl.WSOP;


namespace LicPdaService
{
    public class PDAXmlSkuCheckProcess
    {
        /// <summary>
        /// 9.0 料号有效性判定 Add By Quinn.Ling 20171018
        /// DataSource:Inventory
        /// </summary>
        public static string GetSkuCheckData(XmlDocument doc)
        {
            #region 通用变量定义
            string docResult = string.Empty; //返回值
            string userid = string.Empty;
            string whseid = string.Empty;
            string master_label = string.Empty;
            string sku = string.Empty;
            string label_qty = string.Empty;
            string qty = "0";
            string return_status = string.Empty;
            List<string> sqllists = new List<string>();
            string strSql = string.Empty;
            string batch = string.Empty;

            XmlDocument docreturn = new XmlDocument();//返回报文
            XmlDeclaration dec = docreturn.CreateXmlDeclaration("1.0", "utf-8", null);//建立Xml的定义声明  
            docreturn.AppendChild(dec);
            XmlElement rootResult = docreturn.CreateElement("NewDataSet"); //创建根节点
            #endregion

            try
            {
                #region 参数源
                App.DBconSet();
                XmlNode root = doc.DocumentElement; // 报文根节点
                XmlNode nodeSku = root.SelectSingleNode("sku");
                XmlNodeList xmlnodelist = nodeSku.ChildNodes;
                try
                {
                    userid = nodeSku.SelectSingleNode("userid").InnerText;
                    whseid = nodeSku.SelectSingleNode("whseid").InnerText;
                    master_label = nodeSku.SelectSingleNode("master_label").InnerText;
                    try
                    {
                        string[] s = master_label.Split(new char[] { '|' });
                        if (s.Length > 2)
                        {
                            sku = s[0].ToString();
                            label_qty = s[3].ToString();
                            batch = s[2].ToString();
                        }
                        else
                        {
                            sku = master_label;
                        }

                    }
                    catch (Exception ex)
                    {
                        App.Log("PDAXmlPrePickProcess.cs-2", ex.Message.ToString());
                    }

                    //单号有效性查询
                    strSql = "SELECT whseid,customer_code,sku FROM Sku where sku='"+sku+"' ";
                    DataTable dt = App.GetDataTable(App.DBconWMS, strSql, "Inventory");
                    if (dt.Rows.Count > 0)
                    {
                        return_status = "1";
                    }
                    else
                    {
                        return_status = "0";
                    }
                }
                catch (Exception ex)
                {
                    App.Log("PDAXmlPrePickProcess.cs-3", ex.Message.ToString());
                }

                #endregion

                #region 构建XML报文
                XmlNode sku_result = docreturn.CreateElement("sku_return");

                XmlElement useridResult = docreturn.CreateElement("userid");
                useridResult.InnerText = userid;
                sku_result.AppendChild(useridResult);

                XmlElement whseidResult = docreturn.CreateElement("whseid");
                whseidResult.InnerText = whseid;
                sku_result.AppendChild(whseidResult);

                XmlElement master_labelResult = docreturn.CreateElement("master_label");
                master_labelResult.InnerText = master_label;
                sku_result.AppendChild(master_labelResult);

                XmlElement skuResult = docreturn.CreateElement("sku");
                skuResult.InnerText = sku;
                sku_result.AppendChild(skuResult);

                XmlElement batchResult = docreturn.CreateElement("batch");
                batchResult.InnerText = batch;
                sku_result.AppendChild(batchResult);

                XmlElement label_qtyResult = docreturn.CreateElement("label_qty");
                label_qtyResult.InnerText = label_qty;
                sku_result.AppendChild(label_qtyResult);

                XmlElement qtyResult = docreturn.CreateElement("qty");
                qtyResult.InnerText = qty;
                sku_result.AppendChild(qtyResult);

                XmlElement return_statusResult = docreturn.CreateElement("return_status");
                return_statusResult.InnerText = return_status;
                sku_result.AppendChild(return_statusResult);

                rootResult.AppendChild(sku_result);
                #endregion
            }
            catch (Exception ex)
            {
                App.Log("PDAXmlPrePickProcess.cs-1", ex.Message.ToString());
            }
            docreturn.AppendChild(rootResult);
            return docreturn.InnerXml;
        }
    }
}
